package user

import (
	"time"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
	"gitee.com/xiaoyutab/xgotool/individual/xginplus"
	"gitee.com/xiaoyutab/xgotool/individual/xqueue"
	"gitee.com/xiaoyutab/xgotool/xstring"
)

// 修改用户密码
//
//	POST	/user/updatePassword
func UpdatePassword(c *xginplus.Context) *xginplus.Response {
	type temp_struct struct {
		ConfirmPassword string `json:"confirmPassword" form:"confirmPassword" xml:"confirmPassword"` // 确认密码
		NewPassword     string `json:"newPassword" form:"newPassword" xml:"newPassword"`             // 新密码
		OldPassword     string `json:"oldPassword" form:"oldPassword" xml:"oldPassword"`             // 原密码
	}
	temp := temp_struct{}
	if err := c.Bind(&temp); err != nil {
		return c.Error(err)
	}
	type temp_struct_used struct {
		ConfirmPassword string // 确认密码
		NewPassword     string // 新密码
		OldPassword     string // 原密码
	}
	param := temp_struct_used{}
	// 参数赋值
	param.ConfirmPassword = temp.ConfirmPassword
	param.NewPassword = temp.NewPassword
	param.OldPassword = temp.OldPassword
	// ApiPost软件所需要的参数描述格式:
	// [{"is_checked":1,"not_null":1,"key":"confirmPassword","type":"Text","field_type":"String","value":"","description":"确认密码"},{"is_checked":1,"not_null":1,"key":"newPassword","type":"Text","field_type":"String","value":"","description":"新密码"},{"is_checked":1,"not_null":1,"key":"oldPassword","type":"Text","field_type":"String","value":"","description":"原密码"}]
	// ApiFox软件所需要的参数描述格式:（此软件需要的参数为多行形式，请手动去除最前面的//和空格，再行复制）
	// "confirmPassword","string","true","","","确认密码"
	// "newPassword","string","true","","","新密码"
	// "oldPassword","string","true","","","原密码"
	// 其他软件所需要的KV键值对描述:
	// {"confirmPassword":"确认密码","newPassword":"新密码","oldPassword":"原密码"}
	// 本命令执行参数【留做备份，以便后期使用/修改】
	// confirmPassword string 确认密码 v=required newPassword string 新密码 v=required oldPassword string 原密码 v=required x.ginplus
	// 参数效验部分
	if param.ConfirmPassword == "" {
		return c.Errorf("确认密码(confirmPassword) 不能为空")
	}
	if param.NewPassword == "" {
		return c.Errorf("新密码(newPassword) 不能为空")
	}
	if param.OldPassword == "" {
		return c.Errorf("原密码(oldPassword) 不能为空")
	}

	if param.NewPassword != param.ConfirmPassword {
		return c.Errorf("新密码与确认密码不一致")
	}
	conn, err := config.GetConnect()
	if err != nil {
		return c.Error(err)
	}
	mod := model.User{}

	err = conn.Table(mod.TableName()).Where("id", c.GetUint("uid")).Where("status", 1).Order("id ASC").First(&mod).Error
	if err != nil {
		return c.Error(err)
	}
	vcode := mod.Password[32:]
	if mod.Password != xstring.MD5(param.OldPassword+vcode)+vcode {
		xqueue.Set("log.user", &model.UserLog{
			Type: 4,
			Uid:  mod.Id,
			Msg:  "旧密码输入错误",
			Ip:   c.ClientIP(),
			Ua:   c.Request.UserAgent(),
			Time: time.Now().Unix(),
		})
		return c.Errorf("用户旧密码输入错误，请重新输入")
	}
	mod.Password = xstring.MD5(param.NewPassword+vcode) + vcode
	err = conn.Table(mod.TableName()).Where("id", mod.Id).Update("password", mod.Password).Error
	if err != nil {
		return c.Error(err)
	}
	xqueue.Set("log.user", &model.UserLog{
		Type: 4,
		Uid:  mod.Id,
		Ip:   c.ClientIP(),
		Ua:   c.Request.UserAgent(),
		Time: time.Now().Unix(),
	})
	return c.SuccessEmpty()
}
